﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CodeCreator.CoreObj
{
    public class Obj
    {
        private string packageName;//PluginName
        private string tableFullName;//TableName,SchemaName
        private string className;//ClassNameAllLower,ClassNameAllUpper,ClassNameFirstLower,ClassNameFirstUpper
        private List<ObjField> fields = new List<ObjField>();
        private List<ObjSql> sqls = new List<ObjSql>();

        public List<ObjSql> Sqls
        {
            get { return sqls; }
            set { sqls = value; }
        }

        public string PackageName
        {
            get { return packageName; }
            set { packageName = value; }
        }


        public string TableFullName
        {
            get { return tableFullName; }
            set { tableFullName = value; }
        }      

        public string TableName
        {
            get
            {
                string result = tableFullName;
                if (tableFullName.IndexOf('.') > 0)
                {
                    result = tableFullName.Substring(tableFullName.IndexOf('.') + 1, tableFullName.Length - tableFullName.IndexOf('.') - 1);
                }

                return result;
            }
        }

        public string PkClassFieldNameFirstUpper
        {
            get
            {
                if (this.Fields != null)
                {
                    int pkCount = 0;
                    string strValue = "";
                    foreach (ObjField of in this.fields)
                    {
                        if (of.DbFieldPk)
                        {
                            pkCount += 1;
                            if (of.DbFieldType == "NUMBER")
                            {
                                strValue = of.ClassFieldNameFirstUpper;
                            }
                        }
                    }
                    if (pkCount == 1)
                        return strValue;
                    else
                        return "";
                }
                else
                    return "";
            }
        }


        public string PkDbFieldName
        {
            get
            {
                if (this.Fields != null)
                {
                    int pkCount = 0;
                    string strValue = "";
                    foreach (ObjField of in this.fields)
                    {
                        if (of.DbFieldPk)
                        {
                            pkCount += 1;
                            if (of.DbFieldType == "NUMBER")
                            {
                                strValue = of.DbFieldName;
                            }
                        }
                    }
                    if (pkCount == 1)
                        return strValue;
                    else
                        return "";
                }
                else
                    return "";
            }
        }

        public string PkClassFieldNameFirstLower
        {
            get
            {
                if (this.Fields != null)
                {
                    int pkCount = 0;
                    string strValue = "";
                    foreach (ObjField of in this.fields)
                    {
                        if (of.DbFieldPk)
                        {
                            pkCount += 1;
                            if (of.DbFieldType == "NUMBER")
                            {
                                strValue = of.ClassFieldNameFirstLower;
                            }
                        }
                    }
                    if (pkCount == 1)
                        return strValue;
                    else
                        return "";
                }
                else
                    return "";
            }
        }

        public string SchemaName
        {
            get
            {
                string result = tableFullName;
                if (tableFullName.IndexOf('.') > 0)
                {
                    result = tableFullName.Substring(0,tableFullName.IndexOf('.') );
                }
                return result;
            }
        }


        public string ClassName
        {
            get { return className; }
            set { className = value; }
        }


        public string ClassNameFirstUpper
        {
            get
            {
                return Common.StrFirstToUpper(className);
            }
        }

        public string PluginName
        {
            get
            {
                if (this.packageName.IndexOf('.') > 0)
                {
                    return this.packageName.Substring(this.packageName.LastIndexOf('.') + 1, this.packageName.Length - this.packageName.LastIndexOf('.') - 1);
                }
               else
                    return this.packageName;
            }
        }

        public string ClassNameFirstLower
        {
            get
            {
                return Common.StrFirstToLower(className);
            }
        }

        public string ClassNameAllLower
        {
            get
            {
                return className.ToLower() ;
            }
        }

        public string ClassNameAllUpper
        {
            get
            {
                return className.ToUpper();
            }
        }


        internal List<ObjField> Fields
        {
            get { return fields; }
            set { fields = value; }
        }
    }
}
